package com.example.astronautcontact0123456789hwh1104;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ContactDetailActivity extends AppCompatActivity {

    private TextView tvContactDetailName;
    private ImageView ivContactDetailCall;
    private TextView tvContactDetailPhone;
    private TextView tvContactDetailOrganization;
    private TextView tvContactDetailEmail;
    private TextView tvContactDetailNotes;
    private ImageView ivContactDetailMark;
    private ImageView ivContactDetailEdit;
    private ImageView ivContactDetailMore;
    private ContactDAL contactDAL=new ContactDAL(this);
    private AstronautContact contact;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_contact_detail);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        id=getIntent().getIntExtra("id",-1);
        if (id!=-1){
            contact=contactDAL.queryById(id);
            tvContactDetailName.setText(contact.getName());
            tvContactDetailPhone.setText(contact.getPhone());
            tvContactDetailOrganization.setText(contact.getOrganization());
            tvContactDetailEmail.setText(contact.getEmail());
            tvContactDetailNotes.setText(contact.getNotes());
        }
        ivContactDetailCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Intent.ACTION_DIAL);
                intent.setData(android.net.Uri.parse("tel:"+contact.getPhone()));
                startActivity(intent);
            }
        });
        ivContactDetailEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.dialog_contact, null);
                ((EditText)view.findViewById(R.id.edtTxtDialogName)).setText(contact.getName());
                ((EditText)view.findViewById(R.id.edtTxtDialogPhone)).setText(contact.getPhone());
                ((EditText)view.findViewById(R.id.edtTxtDialogEmail)).setText(contact.getEmail());
                ((EditText)view.findViewById(R.id.edtTxtDialogOrganization)).setText(contact.getOrganization());
                ((EditText)view.findViewById(R.id.edtTxtDialogNotes)).setText(contact.getNotes());
                new android.app.AlertDialog.Builder(ContactDetailActivity.this)
                        .setTitle("编辑联系人")
                        .setView(view)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String name = ((EditText)view.findViewById(R.id.edtTxtDialogName)).getText().toString().trim();
                                String phone = ((EditText)view.findViewById(R.id.edtTxtDialogPhone)).getText().toString().trim();
                                String email = ((EditText)view.findViewById(R.id.edtTxtDialogEmail)).getText().toString().trim();
                                String organization = ((EditText)view.findViewById(R.id.edtTxtDialogOrganization)).getText().toString().trim();
                                String notes = ((EditText)view.findViewById(R.id.edtTxtDialogNotes)).getText().toString().trim();
                                AstronautContact contactUpdate = new AstronautContact(id,name,phone,email,organization,notes);
                                if(contactDAL.update(contactUpdate)!=-1){
                                    contact=contactDAL.queryById(id);
                                    tvContactDetailName.setText(contact.getName());
                                    tvContactDetailPhone.setText(contact.getPhone());
                                    tvContactDetailOrganization.setText(contact.getOrganization());
                                    tvContactDetailEmail.setText(contact.getEmail());
                                    tvContactDetailNotes.setText(contact.getNotes());
                                    Toast.makeText(ContactDetailActivity.this,"更新成功",Toast.LENGTH_SHORT);
                                }else {
                                    Toast.makeText(ContactDetailActivity.this,"更新失败",Toast.LENGTH_SHORT);
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create()
                        .show();

            }
        });
        ivContactDetailMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(ContactDetailActivity.this, v);
                popupMenu.getMenuInflater().inflate(R.menu.contact_popup_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(
                        new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem menuItem) {
                                int itemId = menuItem.getItemId();
                                if(itemId==R.id.itemMenuDel){
                                    new AlertDialog.Builder(ContactDetailActivity.this)
                                            .setTitle("确定删除该联系人？")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    contactDAL.delete(id);
                                                    startActivity(new Intent(ContactDetailActivity.this,MainActivity.class));
                                                    finish();
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener(){
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    dialogInterface.dismiss();
                                                }
                                            })
                                            .create()
                                            .show();
                                    return true;
                                }else if(itemId==R.id.itemMenuAdd){

                                    return true;
                                }else if(itemId==R.id.itemMenuMark){
                                    View view = getLayoutInflater().inflate(R.layout.dialog_memo,null);
                                    EditText edtTxtDialogMemo=((EditText)view.findViewById(R.id.edtTxtDialogMemo));
                                    SharedPreferences sp=getSharedPreferences("memo",MODE_PRIVATE);
                                    edtTxtDialogMemo.setText(sp.getString(String.valueOf(contact.getId()),""));

                                    new AlertDialog.Builder(ContactDetailActivity.this)
                                            .setView(view)
                                            .setTitle("联系人备忘录")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    SharedPreferences sp=getSharedPreferences("memo",MODE_PRIVATE);
                                                    String memo=edtTxtDialogMemo.getText().toString().trim();
                                                    sp.edit().putString(String.valueOf(contact.getId()),memo).apply();
                                                    Toast.makeText(ContactDetailActivity.this,"成功添加备忘录",Toast.LENGTH_SHORT).show();

                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    dialogInterface.dismiss();
                                                }
                                            })
                                            .create()
                                            .show();

                                    return true;
                                }else {
                                    return false;
                                }

                            }
                        }
                );
                popupMenu.show();

            }
        });

    }

    private void initView() {
        tvContactDetailName = (TextView) findViewById(R.id.tvContactDetailName);
        ivContactDetailCall = (ImageView) findViewById(R.id.ivContactDetailCall);
        tvContactDetailPhone = (TextView) findViewById(R.id.tvContactDetailPhone);
        tvContactDetailOrganization = (TextView) findViewById(R.id.tvContactDetailOrganization);
        tvContactDetailEmail = (TextView) findViewById(R.id.tvContactDetailEmail);
        tvContactDetailNotes = (TextView) findViewById(R.id.tvContactDetailNotes);
        ivContactDetailMark = (ImageView) findViewById(R.id.ivContactDetailMark);
        ivContactDetailEdit = (ImageView) findViewById(R.id.ivContactDetailEdit);
        ivContactDetailMore = (ImageView) findViewById(R.id.ivContactDetailMore);
    }
}